<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Category
 *
 * @author Administrator
 */
class Category extends CActiveRecord {

    public function rules() {
        return array(
            array('category_name, category_parent_id', 'required'),
            array('category_name', 'length', 'max' => 35, 'min' => 1),
            array('category_keywords,category_description,category_title', 'length', 'max' => 250)
        );
    }

    public function attributeLabels() {
        return array(
            'category_id' => Yii::t('default', '编号'),
            'category_name' => Yii::t('default', '分类名称'),
            'category_parent_id' => Yii::t('default', '登录密码'),
            'category_keywords' => Yii::t('default', '页面关键词'),
            'category_title' => Yii::t('default', '页面标题'),
            'category_sort' => Yii::t('default', '显示顺序'),
            'category_alias' => Yii::t('default', '分类别名'),
            'category_description' => Yii::t('default', '分类描述'),
            'category_children_id' => Yii::t('default', '子分类编号'),
            'category_children_num' => Yii::t('default', '子分类数'),
            'category_add_title' => Yii::t('default', '新增分类目录'),
            'category_video_num' => Yii::t('default', '视频数'),
            'category_edit_title' => Yii::t('default', '编辑分类信息'),
            'category_error_404' => Yii::t('default', '分类不存在')
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{category}}';
    }
    public function getParentOption() {
        $result = $this->findAll("category_parent_id = :id", array(":id" => 0));
        $data[] = Yii::t("default", Yii::t('default', '设为一级分类'));
        foreach ($result as $row) {
            $data[$row['category_id']] = $row['category_name'];
        }
        return $data;
    }

    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                if (empty($this->category_alias)) {
                    $this->category_alias = Pinyin::out($this->category_name);
                    
                }
            }
            return true;
        } else {
            return false;
        }
    }

    public function afterSave() {
        if ($this->category_parent_id > 0) {
            $parent = $this->findByPk($this->category_parent_id);
            if ($parent) {
                $parent->category_children_num = $parent->category_children_num + 1;
                $parent->save();
            }
        }
    }

    public function beforeDelete() {
        if (parent::beforeDelete()) {
            if ($this->category_video_num > 0 || $this->category_children_num > 0) {
                $this->addError('category_delete', Yii::t('default', '分类下有视频以及子分类时不允许删除，如果要删除请先转移数据'));
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    public function afterDelete() {
        if ($this->category_parent_id) {
            $parent = $this->findByPk($this->category_parent_id);
            if ($parent) {
                $parent->category_children_num = $parent->category_children_num - 1;
                $parent->save();
            }
        }
    }

}